function fid = fid_setup(varargin)
% FID_SETUP Set up various parameters for PARSE FID
%
% FID_SETUP initializes various parameters and arrays, depending on the
% parameter components and values supplied in the structure PSEQ and returns
% the values in another structure FID that includes the PSEQ variables plus
% the calculated parameters.
%
% Note that the origin is the upper left corner.  x is the row variable and
% y is the column variable.

pseq = varargin{1};
if nargin==2,
    Ncpus = varargin{2};
else
    Ncpus = 1;
end

FIDR0 = (pseq.FR+1i*pseq.FI+pseq.dcoff);  % FID with DC offset correction
NF0 = size(pseq.FR,1);
fid.delt = 1/pseq.sw;  % time increment
NF = NF0-pseq.startx-pseq.endx;  % adjusted length of FID
% if (Ncpus > 1)
%     NF = NF - mod(NF,Ncpus);
% end
frno=varargin{3};
% remove experimentally measured eddy current phase
FIDR = FIDR0(pseq.startx+1:NF0-pseq.endx,frno).*exp(-1i*(pseq.phir(pseq.startx+1:NF0-pseq.endx).'));
% twist FID to compensate for off-resonance shift at acquisition
fid.sig = pseq.fidfac*FIDR.'.*exp(2i*pi*pseq.offr*(1:NF)*fid.delt)/max(abs(FIDR));
% calculate trajectory info
kss = pseq.krd+1i*pseq.kpe;
kss=kss(pseq.startx+1:NF0-pseq.endx);
fid.kss=kss;
% if (Ncpus > 1)
%     fid.kx = reshape(real(kss),Ncpus,NF/Ncpus);
%     fid.ky = reshape(imag(kss),Ncpus,NF/Ncpus);
% else
    fid.kx = real(kss);
    fid.ky = imag(kss);
% end
fid.offr=pseq.offr;